package com.cskaoyan.gateway.controller.user;

import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.commons.tool.utils.CookieUtil;
import com.mall.user.ILoginOutService;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dto.UserLoginOutResponse;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @ClassName: LoginOutController
 * @Description:
 * @Author: xiaomi.mzt@qq.com
 * @Date: 2021/5/22 15:00
 */

@RestController
@RequestMapping("user")
public class LoginOutController {

    public static String ACCESS_TOKEN = "access_token";

    @Reference(check = false)
    private ILoginOutService iLoginOutService;

    @GetMapping("loginOut")
    public ResponseData loginOut(HttpServletRequest request, HttpServletResponse response){

        UserLoginOutResponse userLoginOutResponse = iLoginOutService.loginOut();
        Cookie cookie = new Cookie(ACCESS_TOKEN, null);
        cookie.setMaxAge(0);
        cookie.setPath("/");
        CookieUtil.setCookie(response, cookie);
        if (userLoginOutResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(null);
        }
        return new ResponseUtil<>().setErrorMsg(userLoginOutResponse.getMsg());
    }

}
